package com.sino.struts2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.googlecode.jsonplugin.annotations.SMDMethod;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.IPatentManageQxkwService;
import com.sino.business.IStaticDataService;
import com.sino.model.QxkwModel;
import com.sino.model.StaticModel;

public class QxkwAction extends ActionSupport {
	private List qxkwtypeList;

	private List qxkwList;

	IPatentManageQxkwService patentManageQxkw;

	IStaticDataService staticDataService;

	private String qxkwmc;

	private String qxkwtype;// 下拉列表单位类型

	private String typeid;// 推荐单位类型

	private String id;// 推荐单位id

	private QxkwModel qm;// 推荐单位id

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getQxkwtype() {
		return qxkwtype;
	}

	public void setQxkwtype(String qxkwtype) {
		this.qxkwtype = qxkwtype;
	}

	public String getTypeid() {
		return typeid;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}

	public String getQxkwmc() {
		return qxkwmc;
	}

	public void setQxkwmc(String qxkwmc) {
		this.qxkwmc = qxkwmc;
	}

	public void setStaticDataService(IStaticDataService staticDataService) {
		this.staticDataService = staticDataService;
	}

	public String getQxkwType() {
		qxkwtypeList = this.patentManageQxkw.getQxkwType();
		qxkwtypeList.remove(qxkwtypeList.size() - 1);// 去除无推荐单位
		return SUCCESS;
	}

	public String saveQxkwType() {

		this.patentManageQxkw.saveQxkwType(qxkwmc);
		return SUCCESS;
	}

	public String delQxkwType() {

		List qxkwtypeList = this.getStaticDataService().findQxkwList(typeid);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		if (qxkwtypeList.size() >= 1) {
			try {
				PrintWriter out = response.getWriter();

				try {
					response.setContentType("text/html;charset=gb2312");
					out = response.getWriter();
				} catch (IOException ex) {
					throw new RuntimeException();
				}
				out
						.println("<script language='JavaScript' type='text/JavaScript'>");
				out.println("if(confirm(\"该类型有推荐单位不能删除，请与技术人员联系\")){");

				out.println("location.href='getQxkwType.action';}");
				out.println("</script>");

				return null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		this.patentManageQxkw.deleteQxkwType(typeid);
		return SUCCESS;
	}

	public String saveQxkwDo() {
		List qxkwList = this.getStaticDataService().findQxkwList(qxkwtype);
		if (qxkwList.size() < 1) { // 推荐单位类型类没数据
			qxkwtype = qxkwtype + "01";
		} else { // 有类型
			StaticModel sm = (StaticModel) qxkwList.get(qxkwList.size() - 1);

			qxkwtype = String.valueOf(Integer.parseInt(sm.getId()) + 1);// 单位类型号加1
		}
		this.patentManageQxkw.saveQxkw(qxkwmc, qxkwtype);
		return SUCCESS;
	}

	public String modifyQxkw() {
		qxkwtypeList = this.getPatentManageQxkw().getQxkwType();
		QxkwModel qmy = new QxkwModel();
		qmy.setId(id);
		qm = this.getPatentManageQxkw().getQxkwById(qmy);
		qm.setTypeid(typeid);

		return SUCCESS;
	}

	public String modifyQxkwDo() {
	
			this.getPatentManageQxkw().updateQxkw(qm);
		
		return SUCCESS;
	}

	public String delQxkw() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out;
		if (!this.getPatentManageQxkw().deleteQxkw(id)) {
			try {
				out = response.getWriter();
				try {
					//response.setContentType("text/html;charset=gb2312");
					out = response.getWriter();
				} catch (IOException ex) {
					throw new RuntimeException();
				}
				out
						.println("<script language='JavaScript' type='text/JavaScript'>");
				out.println("if(confirm(\"该推荐单位已经填报信息不能删除，请与技术人员联系\")){");

				out.println("location.href='getQxkw.action';}");
				out.println("</script>");

				return null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;

	}

	public String verfiyQxkw() {

		return Action.SUCCESS;
	}

	@SMDMethod
	public QxkwModel doSameName(QxkwModel qm) {
		boolean flag = this.getPatentManageQxkw().verifyQxkw(qm);
		if (flag == false) {
			qm.setName("");
		} else {
			qm.setName("user");

		}
		return qm;
	}

	public String saveQxkw() {
		qxkwtypeList = this.getPatentManageQxkw().getQxkwType();

		return SUCCESS;
	}

	public String getQxkw() {
		qxkwList = this.getPatentManageQxkw().getQxkw();
		return SUCCESS;
	}

	public void setQxkwList(List qxkwList) {
		this.qxkwList = qxkwList;
	}

	public IPatentManageQxkwService getPatentManageQxkw() {
		return patentManageQxkw;
	}

	public void setPatentManageQxkw(IPatentManageQxkwService patentManageQxkw) {
		this.patentManageQxkw = patentManageQxkw;
	}

	public IStaticDataService getStaticDataService() {
		return staticDataService;
	}

	public void setStaticDateService(IStaticDataService staticDataService) {
		this.staticDataService = staticDataService;
	}

	public List getQxkwList() {
		return qxkwList;
	}

	public List getQxkwtypeList() {
		return qxkwtypeList;
	}

	public void setQxkwtypeList(List qxkwtypeList) {
		this.qxkwtypeList = qxkwtypeList;
	}

	public QxkwModel getQm() {
		return qm;
	}

	public void setQm(QxkwModel qm) {
		this.qm = qm;
	}
}
